#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project: DataStructure-Python
@File: Single-Linked-List.py
@Author: Dragon Geek
@Date: 2021/4/19 10:15
@Description: Bubble Sort Algorithm
"""


class Node(object):
    # 单链表的节点
    def __init__(self, elem):
        # elem存放数据元素
        self.elem = elem
        # next存放节点标识
        self.next = None


class SingleLinkedList(object):
    # 单链表
    def __init__(self, node=None):
        self.__head = node

    # 链表是否为空
    def is_empty(self):
        return self.__head is None

    # 返回链表的长度
    def length(self):
        # cur游标，用来移动遍历节点
        cur = self.__head
        # count记录数量
        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    # 遍历整个列表
    def travel(self):
        # cur游标，用来移动遍历节点
        cur = self.__head
        while cur is not None:
            print(cur.elem, end=" ")
            cur = cur.next

    # 链表头部增加元素
    def add(self, item):
        node = Node(item)
        node.next = self.__head
        self.__head = node

    # 链表尾部增加元素
    def append(self, item):
        node = Node(item)
        if self.is_empty():
            self.__head = node
        else:
            cur = self.__head
            while cur.next is not None:
                cur = cur.next
            cur.next = node

    """
    指定位置添加元素
    :param position: 从0开始
    """

    def insert(self, position, item):
        if position <= 0:
            self.add(item)
        elif position >= (self.length() - 1):
            self.append(item)
        else:
            node = Node(item)
            pre = self.__head
            count = 0
            while count < position:
                count += 1
                pre = pre.next
            node.next = pre.next
            pre.next = node

    # 删除节点
    def remove(self, item):
        pre = self.__head
        cur = self.__head
        while cur is not None:
            if cur.elem == item:
                # 先判断此节点是否头结点
                if cur == self.__head:
                    self.__head = cur.next
                else:
                    pre.next = cur.next
                break
            else:
                pre = cur
                cur = cur.next

    # 查找节点是否存在
    def search(self, item):
        cur = self.__head
        while cur.next is not None:
            if cur.elem == item:
                return True
            else:
                cur = cur.next
        return False


if __name__ == "__main__":
    SingleLinkedList_01 = SingleLinkedList()
    print(SingleLinkedList_01.is_empty())
    print(SingleLinkedList_01.length())
    SingleLinkedList_01.add(1)
    SingleLinkedList_01.append(1)
    print(SingleLinkedList_01.is_empty())
    print(SingleLinkedList_01.length())
    SingleLinkedList_01.append(2)
    SingleLinkedList_01.append(3)
    SingleLinkedList_01.append(4)
    SingleLinkedList_01.append(5)
    SingleLinkedList_01.append(5)
    SingleLinkedList_01.append(5)
    SingleLinkedList_01.append(5)
    SingleLinkedList_01.append(5)
    SingleLinkedList_01.add(8)
    SingleLinkedList_01.insert(2, 2132132)
    SingleLinkedList_01.insert(-1, 164654)
    SingleLinkedList_01.insert(100, 164654)
    SingleLinkedList_01.travel()
    print("The length of SingleLikedList:", SingleLinkedList_01.length())
    print(SingleLinkedList_01.search(10000))
    SingleLinkedList_01.remove(5)
    SingleLinkedList_01.travel()
    print("The length of SingleLikedList:", SingleLinkedList_01.length())